package com.cby.jetpack.sample.navigation

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.cby.jetpack.sample.R
import com.cby.jetpack.sample.databinding.ActivityNavigationAdvanceBinding
import com.cby.jetpack.sample.ext.binding
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 * @description: NavigationAdvanceActivity
 *
 * @author ChenBaoyang
 * @date 2021/8/14 14:31
 */
class NavigationAdvanceActivity : AppCompatActivity() {

    private lateinit var navController: NavController
    private lateinit var appBarConfiguration: AppBarConfiguration


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation_advance)
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.my_nav_host_fragment)
                as NavHostFragment
        navController = navHostFragment.navController
        //将navigation的控制器绑定到BottomNavigationView
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
        bottomNavigationView.setupWithNavController(navController)

        // 使用navController和3个顶级目标设置ActionBar
        appBarConfiguration = AppBarConfiguration(
                setOf(R.id.titleScreen, R.id.leaderboard2, R.id.register2))
        setupActionBarWithNavController(navController, appBarConfiguration)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration)
    }
}